<template>
  <div class="hospitalsign">
    <div class="top">
      <div class="title">参加社会活动信息</div>
      <div class="topinfo">
        <div class="post">参加的社会活动类型</div>
        <div :class="{'empty-style': !form.hdlx}" @click="showPicker('hdlx')">{{form.hdlx || '请选择'}}</div>
      </div>
      <div class="topinfo">
        <div class="post">活动开始时间</div>
        <div :class="{'empty-style': !form.kssj}" @click="showDatePicker('kssj')">{{form.kssj | formatDate}}</div>
      </div>
      <div class="topinfo">
        <div class="post">活动结束时间</div>
        <div :class="{'empty-style': !form.jssj}" @click="showDatePicker('jssj')">{{form.jssj | formatDate}}</div>
      </div>
      <div class="topinfo">
        <div class="post">离京情况</div>
        <div :class="{'empty-style': !form.ljqk}" @click="showPicker('ljqk')">{{form.ljqk || '请选择'}}</div>
      </div>
      <div class="topinfo" v-if="ljqkFlag">
        <div class="post">目的地</div>
        <div>
          <input
            type="text"
            placeholder="请输入"
            v-model="form.mdd"
          />
        </div>
      </div>
      <div class="topinfo" v-if="ljqkFlag">
        <div class="post">离京开始时间</div>
        <div :class="{'empty-style': !form.ljkssj}" @click="showDatePicker('ljkssj')">{{form.ljkssj | formatDate}}</div>
      </div>
      <div class="topinfo" v-if="ljqkFlag">
        <div class="post">离京结束时间</div>
        <div :class="{'empty-style': !form.ljjssj}" @click="showDatePicker('ljjssj')">{{form.ljjssj | formatDate}}</div>
      </div>
      <div class="topinfo">
        <div class="post">参加事由</div>
        <div>
          <input
            type="text"
            placeholder="请输入"
            v-model="form.cjsy"
          />
        </div>
      </div>
      <div class="topinfo" style="border-bottom: none;">
        <div class="post">图片</div>
        <div>
          <van-uploader :preview-image="false" v-model="fileList" :after-read="afterRead">
            <van-button icon="plus" type="primary"><span class="ploadSies">上传文件</span></van-button>
          </van-uploader>
        </div>
      </div>
      <div class="topinfo noborder" v-show="fileList.length">
        <div v-for="(val,index) in fileList" :key="index">
          <span class="ploadSies">{{val.file.name}}</span><van-icon name="cross" @click="removeImage(val)" />
        </div>
        <div></div>
      </div>
    </div>
    <div class="sign-btn" @click="submit">提交</div>
    <div class="historylist">
      <p class="historytitle">历次活动记录</p>
      <div class="item" v-for="(item, i) in list" :key="i">
        <div>
          <p>活动日期：{{ item.createTime }}</p>
          <p>{{ item.hdlx }}</p>
        </div>
        <div @click="itemHf(item)">{{item.isBm === 0 ? '填写社会活动报告' : '已填社会活动报告'}}</div>
      </div>
    </div>
    <picker
      :columns='pickerOptions.pickerColumns'
      @fetchData='setData' :show='show'
      @cancel='hidePicker'
      @hide='hidePicker'>
    </picker>
    <date-picker
      @fetchData="fetchDate"
      date-type="date"
      :show="isDatePickerShow"
      @cancel="hideDatePicker"
      @hide="hideDatePicker"
    ></date-picker>
  </div>
</template>

<script>
import datePicker from '../components/datePicker';
import picker from '../components/picker';
import {shhdxxsavedata, shhdxxquerydata4list, syscodecontrollergetcjshhdlx, uploadoneattachment} from '../assets/js/api';
import moment from 'moment';
export default {
  filters: {
    formatDate(v) {
      return (v && moment(v).format('YYYY-MM-DD')) || '请选择';
    }
  },
  components: {
    datePicker,
    picker
  },
  data() {
    return {
      form: {
        ljqk: '',
        hdlx: '',
        cjsy: '',
        images: [],
        ljkssj: '',
        ljjssj: '',
        mdd: '',
        kssj: '',
        jssj: ''
      },
      hdlxFlag: '',
      ljqkFlag: '',
      show: false,
      isDatePickerShow: false,
      fileList: [],
      acitvityTypeList: [],
      ljqkList: [
        {
          label: '是',
          value: 1
        },
        {
          label: '否',
          value: 0
        }
      ],
      list: [],
      pickerOptions: {
        pickerColumns: []
      },
      keys: ['ljqk', 'hdlx', 'cjsy', 'images', 'kssj', 'jssj'],
      dspToken: localStorage.dspToken
    };
  },
  mounted() {
    this.getHistoryList();
    this.getSyscodecontrollergetcjshhdlx();
  },
  watch: {
    ljqkFlag: {
      handler(newV, oldV) {
        if (newV === 1) {
          this.keys = Object.keys(this.form);
        } else {
          this.keys = ['hdsj', 'ljqk', 'hdlx', 'cjsy', 'images'];
        }
      }
    }
  },
  methods: {
    itemHf(item) {
      if (item.isBm !== 0) { return; }
      this.$router.push({
        name: 'ReturnVisitForm',
        query: {
          title: '社会活动报告',
          method: 'activity',
          id: item.hdxxId
        }
      });
    },
    getSyscodecontrollergetcjshhdlx() {
      syscodecontrollergetcjshhdlx({
        dspToken: this.dspToken
      }).then(res => {
        this.acitvityTypeList = res.data.map(x => ({
          label: x.name,
          value: x.val
        }));
      });
    },
    submit() {
      let FIELD_MAP = {
        hdlx: '参加的社会活动类型',
        kssj: '活动开始时间',
        jssj: '活动结束时间',
        ljqk: '离京情况',
        cjsy: '参加事由'
      };
      if (this.ljqkFlag) {
        FIELD_MAP.ljkssj = '离京开始时间';
        FIELD_MAP.ljjssj = '离京结束时间';
        FIELD_MAP.mdd = '目的地';
      }
      const params = {
          ljqk: this.form.ljqk,
          hdlx: this.form.hdlx,
          cjsy: this.form.cjsy,
          images: this.form.images,
          ljkssj: this.form.ljkssj,
          ljjssj: this.form.ljjssj,
          mdd: this.form.mdd,
          kssj: this.form.kssj,
          jssj: this.form.jssj
        }, key = Object.keys(FIELD_MAP).find(key => !params[key]);
      if (key) {
        this.$toast(`请填写【${FIELD_MAP[key]}】项内容`);
        return;
      }

      if (new Date(this.kssj) > new Date(this.jssj)) { return this.$toast('开始时间不能晚于结束时间'); }
      shhdxxsavedata({
        hdlx: this.hdlxFlag,
        hdsj: moment(this.form.kssj).format('YYYY-MM-DD'),
        hdjssj: moment(this.form.jssj).format('YYYY-MM-DD'),
        ljqk: this.ljqkFlag,
        cjsy: this.form.cjsy,
        dspToken: this.dspToken,
        ljkssj: moment(this.form.ljkssj).format('YYYY-MM-DD'),
        ljjssj: moment(this.form.ljjssj).format('YYYY-MM-DD'),
        images: this.form.images.join(','), // 图片数组
        mdd: this.form.mdd
      }).then(({pkValue}) => {
        this.uploadoneattachment(pkValue);
      });
    },
    getHistoryList() {
      shhdxxquerydata4list({
        dspToken: this.dspToken
      }).then(res => {
        this.list = res.data;
      });
    },
    removeImage(val) {
      this.fileList = this.fileList.filter((item, index) => {
        if (val.content === item.content) {
          this.form.images.splice(index, 1);
        } else {
          return item;
        }
      });
    },
    afterRead(file) {},
    async uploadoneattachment(pkValue) {
      for (let item of this.fileList) {
        let formData = new FormData();
        formData.append('file', item.file);
        formData.append('busiId', pkValue || 'jggb');
        formData.append('dspToken', this.dspToken);
        formData.append('busiTable', 'T_LGB_SHJZGL_JZXX');
        formData.append('busiType', 'jggb_shhdtp');
        await uploadoneattachment(formData).then(res => {
          console.log(res);
        });
      }
      this.fileList = [];
      this.$toast('提交成功');
      this.getHistoryList();
      this.form = {
        ljqk: '',
        hdlx: '',
        cjsy: '',
        images: [],
        ljkssj: '',
        ljjssj: '',
        mdd: '',
        kssj: '',
        jssj: ''
      };
    },
    showDatePicker(type) {
      this.dateType = type;
      this.isDatePickerShow = true;
    },
    fetchDate(d) {
      this.form[this.dateType] = d.originValue;
      this.isDatePickerShow = d.showDatePicker;
    },
    hideDatePicker(d) {
      this.isDatePickerShow = d.showDatePicker;
    },
    showPicker(type) {
      this.type = type;
      this.show = true;
      this.pickerOptions.pickerColumns = type === 'ljqk' ? this.ljqkList : this.acitvityTypeList;
    },
    hidePicker(d) {
      this.show = d.showPicker;
    },
    setData(d) {
      if (this.type === 'ljqk') {
        this.form.ljqk = d.value.label;
        this.ljqkFlag = d.value.value;
      } else {
        this.form.hdlx = d.value.label;
        this.hdlxFlag = d.value.value;
      }
      this.show = d.showPicker;
    }
  }
};
</script>

<style lang="less" scoped>
.hospitalsign {
  padding: 24px;
  background: #f5f5f5;
  .top {
    width: 704px;
    background-color: #ffffff;
    box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
    border-radius: 8px;
    margin: 0 auto;
    .title {
      height: 64px;
      background-color: #3198ed;
      color: #ffffff;
      font-size: 30px;
      line-height: 64px;
      padding: 0 16px;
      border-top-left-radius: 8px;
      border-top-right-radius: 8px;
    }
    .topinfo {
      display: flex;
      padding: 35px 18px;
      box-sizing: border-box;
      width: 674px;
      margin: 0 auto;
      font-size: 34px;
      color: #343434;
      &:not(:last-child, .noborder) {
        border-bottom: 1px solid #eeeeee;
      }
      .ploadSies {
        font-size: 25px;
      }
      .post {
        color: #888888;
        text-align: left;
      }
      div:last-child {
        flex: 1;
        text-align: right;
        input {
          text-align: right;
        }
      }
      .special {
        width: 380px;
      }
      .arrow {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        font-size: 34px;
        color: #888;
        text-align: right;
      }
      .empty-style{
        color: #bfbfbf;
      }
      /deep/ .van-uploader__wrapper {
        .van-uploader__upload {
          width: 156px;
          height: 156px;
          background-color: #bfbfbf;
          .van-icon {
            font-size: 40px;
            color: #fff;
            &:before {
              content: "\F0A2";
            }
          }
        }
        .van-uploader__mask {
          .van-icon {
            font-size: 40px;
            color: #fff;
          }
        }
        .van-uploader__preview-image {
          width: 156px;
          height: 156px;
          .van-icon {
            font-size: 40px;
            color: #fff;
            &:before {
              content: "\F0A2";
            }
          }
        }
        .van-uploader__preview-delete {
          width: 30px;
          height: 30px;
          border-radius: 15px;
          .van-uploader__preview-delete-icon {
            position: absolute;
            font-size: 12px;
            right: 2px;
            top: 2px;
            transform: scale(0.6);
          }
        }
      }
    }
    .noborder {
      padding-top: 0;
      font-size: 20px;
      display: block;
      &>div {
        width: 100%;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
    }
  }
  .sign-btn {
    border: none;
    width: 704px;
    height: 90px;
    background-color: #3198ed;
    box-shadow: 0px 5px 5px 0px rgba(89, 166, 250, 0.1);
    border-radius: 45px;
    line-height: 90px;
    color: #ffffff;
    font-size: 36px;
    text-align: center;
    margin: 26px auto;
    letter-spacing: 7px;
  }
  .historylist {
    .historytitle {
      color: #888888;
      font-size: 30px;
      margin: 19px 0;
    }
    .item {
      width: 704px;
      // height: 140px;
      background-color: #ffffff;
      box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
      border-radius: 8px;
      margin: 0 auto;
      margin-top: 23px;
      position: relative;
      padding: 31px 30px 26px 33px;
      box-sizing: border-box;
      display: flex;
      &::before {
        content: "";
        width: 5px;
        // height: 140px;
        background-color: #3198ed;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        border-top-left-radius: 50px;
        border-bottom-left-radius: 50px;
      }
      > div:first-child {
        flex: 1;
        padding-right: 10px;
        p:first-child {
          color: #888888;
          font-size: 30px;
        }
        p:last-child {
          // width: calc(100vw - 160px);
          color: #353535;
          font-size: 34px;
          // white-space: nowrap;
          // text-overflow: ellipsis;
          // overflow: hidden;
        }
      }
      > div:last-child {
        font-size: 24px;
        color: #b2b2b2;
        align-self: center;
        text-align: right;
        background-color: #d0d0d0;
        border-radius: 29px;
        padding: 0 20px;
        color: #353535;
      }
    }
  }
}

.van-button--primary {
  background-color: #ccc;
  border: 1px solid #ccc;
  border-radius: 50px;
}
</style>
